/**
 * @file		
 * @version		v1.0.0
 * @author		Roamboy
 * @copyright	GPL version 3
 * @brief		
 * @note		
 * 				-# 
 */

#include <ctype.h>

#include "sys_utils.h"

const char n_map_ascii[16] =
		{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
				'E', 'F' };

/**
 * @brief		
 * @param		
 * @return		
 * @version		v1.0.0
 * @note		
 * 				-# 
 * @par example:
 * @code
 * //
 * @endcode
 */

/*----------------------------------------------------------------------------*/
/**
 * @brief  Converts a 2 digit decimal to BCD format.
 * @param  Value: Byte to be converted
 * @retval Converted byte
 */
uint8_t byte_to_bcd2(uint8_t value)
{
	uint32_t bcdhigh = 0U;

	while (value >= 10U)
	{
		bcdhigh++;
		value -= 10U;
	}

	return ((uint8_t) (bcdhigh << 4U) | value);
}

/**
 * @brief  Converts from 2 digit BCD to Binary.
 * @param  Value: BCD value to be converted
 * @retval Converted word
 */
uint8_t bcd2_to_byte(uint8_t value)
{
	uint32_t tmp = 0U;
	tmp = ((uint8_t) (value & (uint8_t) 0xF0U) >> (uint8_t) 0x4U) * 10U;
	return (uint8_t) (tmp + (value & (uint8_t) 0x0FU));
}

/**
 * @brief  Converts a 4 digit decimal to BCD format.
 * @param  Value: Byte to be converted
 * @retval Converted byte
 */
uint16_t bin_to_bcd4(uint16_t bin)
{
	uint16_t retval = 0U;
	uint16_t div = 10000U;

	for (; div > 0; div /= 10)
	{
		retval <<= 4;
		retval |= bin / div;
		bin %= div;
	}

	return (retval);
}

/**
 * @brief  Converts from 4 digit BCD to Binary.
 * @param  Value: BCD value to be converted
 * @retval Converted word
 */
uint16_t bcd4_to_bin(uint16_t bcd4)
{
	uint16_t retval = 0;

	int i;

	for (i = 12; i >= 0; i -= 4)
	{
		retval *= 10;
		retval += (bcd4 >> i) & 0x0F;
	}

	return (retval);
}

/**
 * @brief  Converts a 8 digit decimal to BCD format.
 * @param  Value: Byte to be converted
 * @retval Converted byte
 */
uint32_t bin_to_bcd8(uint32_t bin)
{
	uint32_t retval = 0U;
	uint32_t div = 100000000U;

	for (; div > 0; div /= 10)
	{
		retval <<= 4;
		retval |= bin / div;
		bin %= div;
	}

	return (retval);
}

/**
 * @brief  Converts from 8 digit BCD to Binary.
 * @param  Value: BCD value to be converted
 * @retval Converted word
 */
uint32_t bcd8_to_bin(uint32_t bcd8)
{
	uint32_t retval = 0;

	int i;

	for (i = 28; i >= 0; i -= 4)
	{
		retval *= 10;
		retval += (bcd8 >> i) & 0x0F;
	}

	return (retval);
}

/**
 * @brief  Converts from 16 digit BCD to Binary.
 * @param  Value: BCD value to be converted
 * @retval Converted word
 */
uint64_t bin_to_bcd16(uint64_t bin)
{
	uint64_t retval = 0UL;
	uint64_t div = 10000000000000000UL;

	for (; div > 0; div /= 10)
	{
		retval <<= 4;
		retval |= bin / div;
		bin %= div;
	}

	return (retval);
}

/**
 * @brief  Converts from 16 digit BCD to Binary.
 * @param  Value: BCD value to be converted
 * @retval Converted word
 */
uint64_t bcd16_to_bin(uint64_t bcd16)
{
	uint64_t retval = 0;

	int i;

	for (i = 60; i >= 0; i -= 4)
	{
		retval *= 10;
		retval += (bcd16 >> i) & 0x0F;
	}

	return (retval);
}

/*----------------------------------------------------------------------------*/
int char_to_bin(char *s, int len, int *bin)
{
	int retval = 0;
	int sym = 1;

	if (!s || len <= 0 || !bin)
		return (-1);

	*bin = 0;
	if (s)
	{
		if (*s == '-')
		{
			++s;
			sym = -1;
		}

		while (*s != '\0' && len > 0)
		{
			if (*s >= '0' && *s <= '9')
			{
				*bin *= 10;
				*bin += *s - '0';
			}
			else
				retval = -1;

			++s;
			--len;
		}

		if (sym < 0)
			*bin *= -1;
	}

	return (retval);
}

#ifndef CHAR_FAST
int char_to_bin_s(char *s, int *bin)
{
	if (s == NULL)
		return -1;

	return char_to_bin(s, strlen(s), bin);
}
#endif

int char_to_double(char *s, int len, double *dval)
{
	int retval = 0;
	int sym = 1;
	int div = 0;

	if (!s || len <= 0 || !dval)
		return (-1);

	if (*s == '-')
		sym = -1;

	*dval = 0;
	while (*s != '\0' && len > 0)
	{
		if (*s >= '0' && *s <= '9')
		{
			*dval *= 10;
			*dval += *s - '0';
			if (div)
				div *= 10;
		}
		else if (*s == '.')
			div = 1;
		else
			retval = -1;

		++s;
		--len;
	}

	if (sym < 0)
		*dval *= -1;
	if (div)
		*dval /= div;

	return (retval);
}

#ifndef CHAR_FAST
int char_to_double_s(char *s, double *dval)
{
	if (s == NULL)
		return -1;

	return char_to_double(s, strlen(s), dval);
}
#endif

int hex_to_char(uint8_t *hex, uint16_t len, char *buff, uint16_t size)
{
	int i;
	int j;

	if (!hex || !len || !buff || !size)
		return (-1);

	for (i = 0, j = 0; i < len; ++i)
	{
		if (j >= size - 1)
			break;
		buff[j++] = n_map_ascii[hex[i] >> 4];

		if (j >= size - 1)
			break;
		buff[j++] = n_map_ascii[hex[i] & 0x0F];
	}

	buff[j] = '\0';
	return (0);
}

int char_to_hex(char *s, int len, uint8_t *hex)
{
	int retval = 0;
	uint8_t HEX;

	if (!s || len <= 0 || !hex)
		return (-1);

	if (len & 1)
	{
		retval = -1;
		--len;
	}

	while (len > 0)
	{
		if (*s >= '0' && *s <= '9')
			HEX = *s - '0';
		else if (*s >= 'a' && *s <= 'f')
			HEX = *s - 'a' + 10;
		else if (*s >= 'A' && *s <= 'F')
			HEX = *s - 'A' + 10;
		else
		{
			HEX = 0;
			retval = -1;
		}
		++s;

		HEX <<= 4;
		if (*s >= '0' && *s <= '9')
			HEX |= *s - '0';
		else if (*s >= 'a' && *s <= 'z')
			HEX |= *s - 'a' + 10;
		else if (*s >= 'A' && *s <= 'Z')
			HEX |= *s - 'A' + 10;
		else
			retval = -1;
		++s;

		*hex = HEX;

		++hex;
		len -= 2;
	}

	return (retval);
}

int char_lower(char *s, int len)
{
	if (!s || len <= 0)
		return (-1);

	while (*s && len > 0)
	{
		*s = tolower(*s);
		++s;
		--len;
	}

	return (0);
}

int char_upper(char *s, int len)
{
	if (!s || len <= 0)
		return (-1);

	while (*s && len-- > 0)
	{
		*s = toupper(*s);
		++s;
		--len;
	}

	return (0);
}

int char_strcasestr_i(char *s, char *d)
{
	char chr0;
	char chr1;
	int i;
	int j;
	int len;
	int dlen;

	if (!s || !d)
		return (-1);

	len = strlen(s);
	dlen = strlen(d);
	if (len < dlen)
		return (-1);

	for (i = 0; i < len; ++i)
	{
		for (j = 0; i + j < len && j < dlen; ++j)
		{
			chr0 = s[i + j];
			chr1 = d[j];
			if (chr0 != chr1)
			{
				if (chr0 >= 'A' && chr0 <= 'Z')
				{
					if (chr0 != chr1 - 32)
						break;
				}
				else if (chr0 >= 'a' && chr0 <= 'z')
				{
					if (chr0 != chr1 + 32)
						break;
				}
			}
		}

		if (j == dlen)
			return (i);
	}

	return (-1);
}

char *char_strcasestr(char *s, char *d)
{
	int index = char_strcasestr_i(s, d);

	if (index < 0)
		return (NULL);

	return (&s[index]);
}

int char_strcasecmp(char *s, char *d)
{
	int len;

	if (!s || !d)
		return -1;

	len = strlen(s);
	if (len != strlen(d))
		return -1;

	return char_strncasecmp(s, d, len);
}

int char_strncasecmp(char *s, char *d, int n)
{
	char chr0;
	char chr1;
	int i;
	int len;
	int dlen;

	if (!s || !d || n < 0)
		return -1;

	len = strlen(s);
	dlen = strlen(d);

	if (n > len || n > dlen)
	{
		if (len != dlen)
			return -1;
		n = len;
	}

	if (n <= len && n <= dlen)
	{
		for (i = 0; i < n; ++i)
		{
			chr0 = s[i];
			chr1 = d[i];
			if (chr0 != chr1)
			{
				if (chr0 >= 'A' && chr0 <= 'Z')
				{
					if (chr0 != chr1 - 32)
						break;
				}
				else if (chr0 >= 'a' && chr0 <= 'z')
				{
					if (chr0 != chr1 + 32)
						break;
				}
			}
		}

		if (i == n)
			return 0;
	}
	return -1;
}

char *char_trim(char *s, int len)
{
	int i;
	char *rs = NULL;

	if (!s || len <= 0)
		return (NULL);

	for (i = 0; i < len; ++i)
	{
		if (s[i] != ' ')
		{
			rs = &s[i];
			break;
		}
	}
	for (i = len - 1; i > 0; --i)
	{
		if (s[i] == ' ')
			s[i] = '\0';
		else
			break;
	}

	return (rs);
}

#ifndef CHAR_FAST
char *char_trim_s(char *s)
{
	if (s == NULL)
		return NULL;

	return char_trim(s, strlen(s));
}
#endif

int char_kv(char *s, char *delim, kv_t *kv)
{
	int len;

	if (!s || !delim || !kv)
		return (-1);

	kv->key = s;
	len = strlen(delim);
	if (len == 0)
		return (-1);
	kv->val = strstr(s, delim);
	if (kv->val)
	{
		*kv->val = '\0';
		kv->val += len;
		return (0);
	}
	else
		return (-1);
}

int char_argv(char *s, int len, char *delim, char **argv, int n)
{
	char *sub;
	char *end;
	int dlen;
	int retval = 0;

	if (!s || !delim || !argv || n <= 0)
		return (-1);

	dlen = strlen(delim);
	if (dlen == 0)
		return (-1);
	sub = s;
	end = s + len;
	while (retval < n && sub < end)
	{
		sub = strstr(s, delim);
		if (sub && sub < end)
		{
			if (sub != s)
				argv[retval++] = s;
			*sub = '\0';
			s = sub + dlen;
		}
		else
		{
			if (s < end)
				argv[retval++] = s;

			break;
		}
	}

	return (retval);
}

#ifndef CHAR_FAST
int char_argv_s(char *s, char *delim, char **argv, int n)
{
	if (s == NULL)
		return -1;

	return char_argv(s, strlen(s), delim, argv, n);
}
#endif

/* hh:mm->hh*100+mm
 *  */
int char_to_hhmm(char *s, uint32_t *hhmm)
{
	int i;
	int j;
	int len;
	int v;
	char buff[8];
	char *phh;
	char *pmm;

	if (!s || !hhmm)
		return (-1);

	memset(buff, 0, sizeof(buff));
	len = strlen(s);
	for (i = 0, j = 0; i < len; ++i)
	{
		if ((s[i] >= '0' && s[i] <= '9') || s[i] == ':')
			buff[j++] = s[i];
		else if (s[i] != ' ')
			return (-1);
		if (j >= 5)
			return (-1);
	}

	phh = buff;
	pmm = strstr(phh, ":");
	if (pmm == NULL)
		return (-1);

	*pmm++ = '\0';
	char_to_bin(phh, strlen(phh), &v);
	v %= 24;
	*hhmm = v * 100;

	char_to_bin(pmm, strlen(pmm), &v);
	*hhmm += v % 60;
	return (0);
}

int buff_cmp(void *ptr1, void *ptr2, int len, int ignore)
{
	int i;
	char chr1;
	char chr2;
	char *buff1;
	char *buff2;

	if (!ptr1 || !ptr2 || len < 0)
		return (-1);

	buff1 = (char*) ptr1;
	buff2 = (char*) ptr2;
	for (i = 0; i < len; ++i)
	{
		chr1 = buff1[i];
		chr2 = buff2[i];
		if (chr1 != '*' && chr2 != '*' && chr1 != chr2)/* '*'任意匹配 */
		{
			if (ignore)/* 忽略大小写 */
			{
				if (chr1 >= 'a' && chr1 <= 'z')
				{
					if (chr1 - 32 != chr2)
						return (-1);
				}
				else if (chr1 >= 'A' && chr1 <= 'Z')
				{
					if (chr1 + 32 != chr2)
						return (-1);
				}
				else if (chr2 >= 'a' && chr2 <= 'z')
				{
					if (chr2 - 32 != chr1)
						return (-1);
				}
				else if (chr2 >= 'A' && chr2 <= 'Z')
				{
					if (chr2 + 32 != chr1)
						return (-1);
				}
				else
					return (-1);
			}
			else
				return (-1);
		}
	}

	return (0);
}

uint16_t buffer_append(void *buffer, uint16_t off, uint16_t size, char *s)
{
	uint16_t len;

	if (!buffer || !size)
		return (0);

	if (s == NULL)
	{
		if (off > size)
			off = size;
		return (off);
	}

	len = (uint16_t) strlen(s);
	if (off >= size)
		return (size);
	else if (len > size || off + len > size)
		len = size - off;

	memcpy((char*) buffer + off, s, len);
	off += len;
	return (off);
}

uint16_t buffer_copy(char *buffer, uint16_t size, char *s)
{
	uint16_t len;

	if (!buffer || !size)
		return (0);

	if (s == NULL)
	{
		memset(buffer, 0, size);
		return (0);
	}

	len = (uint16_t) strlen(s);
	if (len >= size)
	{
		memcpy(buffer, s, size - 1);
		buffer[size - 1] = '\0';
		return (size - 1);
	}

	strcpy(buffer, s);
	return (len);
}

uint16_t buffer_cat(char *buffer, uint16_t size, char *s)
{
	uint16_t off;

	if (!buffer || !size)
		return (0);

	off = (uint16_t) strlen(buffer);
	if (s == NULL)
	{
		if (off > size)
		{
			buffer[size - 1] = '\0';
			off = size - 1;
		}
		return (off);
	}

	off = buffer_append(buffer, off, size - 1, s);
	if (off == size - 1)
		buffer[size - 1] = '\0';
	else
		buffer[off] = '\0';
	return (off);
}

int hex2char(uint8_t *hex, uint16_t len, char *str, int size)
{
	uint8_t h4;
	uint8_t l4;
	int i;

	if (!hex || !len || !str || size <= 0)
		return (-1);

	for (i = 0; i * 2 < size - 1 && i < len; ++i)/* HEX 2 ASCII */
	{
		h4 = hex[i] >> 4;
		l4 = hex[i] & 0xF;

		if (h4 < 10)
			str[i * 2] = '0' + h4;
		else
			str[i * 2] = 'A' + h4 - 10;

		if (l4 < 10)
			str[i * 2 + 1] = '0' + l4;
		else
			str[i * 2 + 1] = 'A' + l4 - 10;
	}
	str[i * 2] = '\0';
	return (i * 2);
}

/*----------------------------------------------------------------------------*/
#if (1)
const uint8_t MODBUS_CRC_TAB_H[] =
{ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,
		0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
		0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
		0xC1, 0x81, 0x40 };

const uint8_t MODBUS_CRC_TAB_L[] =
{ 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05,
		0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A,
		0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B,
		0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14,
		0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11,
		0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36,
		0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF,
		0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28,
		0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D,
		0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22,
		0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63,
		0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C,
		0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69,
		0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE,
		0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77,
		0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50,
		0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55,
		0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A,
		0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B,
		0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44,
		0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41,
		0x81, 0x80, 0x40 };

uint16_t MODBUS_crc16(uint8_t *buff, uint16_t len)
{
	int i;
	int ix;
	uint16_t crc;
	uint8_t crc_h = 0xFF;
	uint8_t crc_l = 0xFF;

	for (i = 0; i < len; i++)
	{
		ix = crc_l ^ (*buff++);
		crc_l = crc_h ^ MODBUS_CRC_TAB_H[ix];
		crc_h = MODBUS_CRC_TAB_L[ix];
	}
	crc = crc_h;
	crc <<= 8;
	crc |= crc_l;

	return (crc);
}
#else

static uint16_t MODBUS_crc16(uint8_t *buff, uint16_t len)
{
	uint16_t tempcrc;
	int i;
	uint16_t crc = 0xFFFF;

	while (len-- != 0)
	{
		crc ^= (*buff);
		for (i = 0; i < 8; i++)
		{
			if ((crc & 1) != 0)
			crc = (crc >> 1) ^ 0xA001;
			else
			crc >>= 1;
		}
		buff++;
	}

	return (crc);
}
#endif

/*----------------------------------------------------------------------------*/
#if (CRC16_CCIT_FAST)
const uint16_t CRC16_CCIT_TAB[256] =
{ 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108,
		0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210,
		0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B,
		0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401,
		0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE,
		0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6,
		0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D,
		0xC7BC, 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
		0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5,
		0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC,
		0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4,
		0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD,
		0xAD2A, 0xBD0B, 0x8D68, 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13,
		0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A,
		0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E,
		0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
		0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1,
		0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB,
		0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0,
		0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8,
		0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657,
		0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9,
		0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882,
		0x28A3, 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
		0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E,
		0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07,
		0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D,
		0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74,
		0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 };

uint16_t _crc16_ccit_calc(uint16_t crc16, uint8_t *data, uint16_t len)
{
	uint16_t i, tmp;

	for (i = 0; i < len; ++i)
	{
		tmp = (crc16 >> 8) ^ data[i];
		crc16 = (crc16 << 8) ^ CRC16_CCIT_TAB[tmp];
	}

	return (crc16);
}

#else
#define CRC16_CCIT_MASK	(0x1021) /* CRC16-CITT mask */
uint16_t crc16_ccit_calc(uint8_t *data, uint16_t size)
{
	return (crc16);
}
#endif

/*----------------------------------------------------------------------------*/
